package com.beiding.workordersystem.common.helper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Component
public class SessionHolder {

    @Autowired
    private MySessionContext mySessionContext;

    public void forceOffline(String name) {
        Map<String, HttpSession> sessionMap = mySessionContext.getSessionMap();
        for (HttpSession session : sessionMap.values()) {
            SecurityContext securityContext = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
            if (securityContext.getAuthentication().getName().equals(name)) {
                session.invalidate();
                break;
            }
        }
    }

}
